package com.wbl.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbl.train.common.business.dto.StationDto;
import com.wbl.train.common.business.entity.StationEntity;
import com.wbl.train.business.mapper.StationMapper;
import com.wbl.train.business.service.StationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wbl.train.common.business.vo.StationVo;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.ResultCode;
import com.wbl.train.common.utils.SnowUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车站 服务实现类
 * @author 王博纶
 * @since 2024-02-01
 */
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, StationEntity> implements StationService {

    @Override
    public PageVo<StationEntity> queryStation(StationDto stationDto) {
        // 获取分页所需要的参数
        int page = stationDto.getPage();
        int limit = stationDto.getLimit();

        LambdaQueryWrapper<StationEntity> stationWrapper = new LambdaQueryWrapper<StationEntity>()
                .orderByDesc(StationEntity::getId);
        //创建分页对象
        IPage<StationEntity> stationPage = new Page<>(page,limit);
        //调用分页方法
        IPage<StationEntity> StationPageData = page(stationPage,stationWrapper);
        // 获取查询出来的数据集合
        List<StationEntity> records = StationPageData.getRecords();
        // 将数据塞到PageVo类中并返回出去
        return new PageVo<StationEntity>()
                .setDataList(records)
                .setPageSize(limit).setCurrentPage(page)
                .setTotal((int) StationPageData.getTotal())
                .setPages((int) StationPageData.getPages());
    }

    @Override
    public boolean insertAndUpdateStation(StationEntity station) {
        // 获取当前的时间 --- 用于创建和修改的时候的时间填充
        DateTime now = DateTime.now();
        // 设置乘车人的修改时间
        station.setUpdateTime(now);
        // 判断Id是否为空
        if(ObjectUtil.isNotNull(station.getId())){
            // 根据id查询车站信息
            StationEntity stationEntity = this.getById(station.getId());
            // 当前id对应的无车站信息
            if(ObjectUtil.isNull(stationEntity)){
                throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前车站信息不存在");
            }
            // 更新车站信息
            return this.updateById(station);
        }

        // 通过name进行车站信息的查询
        StationEntity stationEntity = getStationByUniqueKey(station.getName());
        // 查询出的passengerEntity不为空，不允许新增
        if(ObjectUtil.isNotEmpty(stationEntity)){
            throw new ApplicationException(ResultCode.DATA_EXIST,"当前车站信息已存在");
        }
        // 给新增的乘车人设置Id，创建时间和修改时间
        station.setId(SnowUtil.getSnowflakeNextId())
                .setCreateTime(now);
        // 新增乘车人
        return this.save(station);
    }

    /**
     * 通过唯一键查询车站信息
     * @param name 唯一键，车站名称
     * @return station火车车站信息
     */
    private StationEntity getStationByUniqueKey(String name) {
        LambdaQueryWrapper<StationEntity> wrapper = new LambdaQueryWrapper<StationEntity>()
                .eq(StationEntity::getName, name);
        return this.getOne(wrapper);
    }

    @Override
    public List<StationVo> queryAllStation() {
        List<StationEntity> stationEntities = selectAllStation();
        return BeanUtil.copyToList(stationEntities,StationVo.class);
    }

    public List<StationEntity> selectAllStation() {
        LambdaQueryWrapper<StationEntity> trainStationWrapper = new LambdaQueryWrapper<StationEntity>()
                .orderByAsc(StationEntity::getNamePinyin);
        // 获取查询出来的数据集合
        return this.list(trainStationWrapper);
    }
}
